<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.wechat')" prop="wechat">
        <el-input
          v-model="formData.wechat"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.wechat')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.qq')" prop="qq">
        <el-input
          v-model="formData.qq"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.qq')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.homePhone')" prop="homePhone">
        <el-input
          v-model="formData.homePhone"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homePhone')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.homeZipcode')" prop="homeZipcode">
        <el-input
          v-model="formData.homeZipcode"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homeZipcode')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.homeAddress')" prop="homeAddress">
        <el-input
          v-model="formData.homeAddress"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homeAddress')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoPersonal.fields.emergencyContact')"
        prop="emergencyContact"
      >
        <el-input
          v-model="formData.emergencyContact"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyContact')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoPersonal.fields.emergencyPhone')"
        prop="emergencyPhone"
      >
        <el-input
          v-model="formData.emergencyPhone"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyPhone')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoPersonal.fields.emergencyAddress')"
        prop="emergencyAddress"
      >
        <el-input
          v-model="formData.emergencyAddress"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyAddress')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoPersonal.fields.currentUnit')" prop="currentUnit">
        <el-input
          v-model="formData.currentUnit"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.currentUnit')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoPersonal.fields.currentAddress')"
        prop="currentAddress"
      >
        <el-input
          v-model="formData.currentAddress"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.currentAddress')
          "
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {
  UstcStudentInfoPersonalApi,
  UstcStudentInfoPersonalVO
} from '@/api/core/ustcstudentinfopersonal'
import { FormRules } from 'element-plus'

/** 学生个人联系信息 表单 */
defineOptions({ name: 'StudentInfoPersonalForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  wechat: undefined,
  qq: undefined,
  homePhone: undefined,
  homeZipcode: undefined,
  homeAddress: undefined,
  emergencyContact: undefined,
  emergencyPhone: undefined,
  emergencyAddress: undefined,
  currentUnit: undefined,
  currentAddress: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.accountId'),
      trigger: 'blur'
    }
  ],
  wechat: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.wechat'),
      trigger: 'blur'
    }
  ],
  qq: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.qq'),
      trigger: 'blur'
    }
  ],
  homePhone: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homePhone'),
      trigger: 'blur'
    }
  ],
  homeZipcode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homeZipcode'),
      trigger: 'blur'
    }
  ],
  homeAddress: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.homeAddress'),
      trigger: 'blur'
    }
  ],
  emergencyContact: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyContact'),
      trigger: 'blur'
    }
  ],
  emergencyPhone: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyPhone'),
      trigger: 'blur'
    }
  ],
  emergencyAddress: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.emergencyAddress'),
      trigger: 'blur'
    }
  ],
  currentUnit: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.currentUnit'),
      trigger: 'blur'
    }
  ],
  currentAddress: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.currentAddress'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoPersonal.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentInfoPersonalApi.getUstcStudentInfoPersonal(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentInfoPersonalVO
    if (formType.value === 'create') {
      await UstcStudentInfoPersonalApi.createUstcStudentInfoPersonal(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentInfoPersonalApi.updateUstcStudentInfoPersonal(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    wechat: undefined,
    qq: undefined,
    homePhone: undefined,
    homeZipcode: undefined,
    homeAddress: undefined,
    emergencyContact: undefined,
    emergencyPhone: undefined,
    emergencyAddress: undefined,
    currentUnit: undefined,
    currentAddress: undefined
  }
  formRef.value?.resetFields()
}
</script>
